Playfair Cipher
Module 01 / Lesson 05
Detailed Video Explanation
The Matrix Logic
The Playfair cipher is the first practical digraph substitution cipher. It encrypts pairs of letters instead of single units, which hides the frequency of individual letters.
How to Setup:
1. Create a 5x5 grid (Matrix) and fill it with a keyword.
2. Remove duplicate letters and treat 'I' and 'J' as the same letter.
3. Split the message into pairs (if a pair has the same letter, insert 'X').
Example Matrix (Key: MONARCHY):
M O N A R
C H Y B D
E F G I K
L P Q S T
U V W X Z
Encryption Rules:
- Rectangle: If letters are in different rows/cols, form a rectangle and take the letters in the same row but opposite corners.
- Same Row: Replace each letter with the one to its right (circularly).
- Same Column: Replace each letter with the one below it (circularly).
Python Implementation
def find_position(matrix, char):
for r in range(5):
for c in range(5):
if matrix[r][c] == char:
return r, c
return None
def playfair_encrypt(text, key_matrix):
# Logic simplified: Processing pairs through the 3 rules
# Same row: right, Same col: down, Rectangle: swap corners
pass # Full logic available in the learning resources
# Example Logic Illustration:
# HE -> Rectangle corners -> CF
# Message: "HELLO" -> "HE LX LO" -> "CF SU PM"